import { observable, action } from "mobx";
import * as API_MIND from '../../apis/mind';
import window from '../../util';

export default class MindeStore {

    mindData: any = {};
    nodeList: any = {};
    loaded: string;
    @observable params: any = {};
    modeId: string;
    formId: string;
    mindId: string;
    rootId: String;
    defaultFloor: number;
    _jm: any;
    apiFlag: Number = 0;

    /**
     * 前端拖动回调方法
     */
    @action
    saveDragendMind = (arr, old_parent, new_parent, selectd_node) => {
        this.editable();
        this.initMap();
    }

    @action
    jsmindLoading = (end) => {
        if (end) {
            this.initMap();
        }
    }

    /**
     * 初始化思维导图
     */
    @action
    initMap = () => {
        if (this.loaded == "1" && window.jsMind) {
            if (this._jm) {
                this._jm.init();
                this._jm.data.init();
            } else {
                var options = {
                    container: 'jsmind_container',
                    editable: false,
                    theme: 'weaver',
                    support_html: true,
                    layout: {
                        hspace: 150,         // 节点之间的水平间距
                        vspace: 10,          // 节点之间的垂直间距
                        pspace: 13           // 节点收缩/展开控制器的尺寸
                    }
                }
                this._jm = new window.jsMind(options);
            }
            var mind = {
                meta: {},
                format: 'node_tree',
                data: this.mindData
            };
            this._jm.show(mind);
            //this.editable();
        }
    }

    /**
     * 调整拖拽控制级别
     */
    @action
    editable = () => {
        this._jm.enable_edit();
    }

    /**
     * 放大
     */
    @action
    zoomIn = () => {
        if (this._jm) {
            this._jm.view.zoomIn();
        }
    }

    /**
     * 缩小
     */
    @action
    zoomOut = () => {
        if (this._jm) {
            this._jm.view.zoomOut();
        }
    }

    /**
     * 缩小
     */
    @action
    collapse_all = () => {
        if (this._jm) {
            this._jm.collapse_all();
        }
    }

    /**
     * 缩小
     */
    @action
    expand_all = () => {
        if (this._jm) {
            this._jm.expand_all();
        }
    }

    /**
     * 进入页面方法
     */
    @action
    updateParams = (params: any) => {
        this.params = params;
        this.init(params);
    }

    /**
     * 页面初始化
     */
    @action
    init = (params) => {
        this.params = params;
        this.apiFlag = 1;
        API_MIND.initMind(params).then((data) => {
            this.apiFlag = 2;
            this.mindData = data.mindMap;
            this.loaded = data.loaded;
            this.defaultFloor = data.defaultFloor;
            window.saveDragendMind = this.saveDragendMind;
            window.clickTable = this.clickTable;
            this.checkNodeList(data.mindMap);
            this.initMap()
        });
    }

    /**
     * 把树状数据处理成list
     */
    @action
    checkNodeList = (data) => {
        let id = data.id;
        let parent = "";
        let count = data.count;
        let loaded = data.loaded;
        let topic = data.topic;
        let title = data.title;
        let children = data.children;
        this.nodeList[id] = {id, parent, count, loaded, topic, title}
        for (let i = 0; i < children.length; i++) {
            this.checkSubNode(id, children[i]);
        }
    }

    /**
     * 把树状数据处理成list
     */
    @action
    checkSubNode = (parent, data) => {
        let id = data.id;
        let count = data.count;
        let loaded = data.loaded;
        let topic = data.topic;
        let title = data.title;
        let children = data.children;
        this.nodeList[id] = {id, parent, count, loaded, topic, title}
        for (let i = 0; i < children.length; i++) {
            this.checkSubNode(id, children[i]);
        }
    }

    /**
     * 前端点击区块的回调事件  非默认展开子节点加载入口
     */
    @action
    clickTable = (id) => {
        let data = this.nodeList[id];
        if (data) {
            let loaded = data.loaded;
            if (loaded) {
                this._jm.toggle_node(id)
            } else {
                this.initNode(id);
            }

        }
    }

    /**
     * 手动加载子节点数据（防止数据过多一次加载过慢）
     */
    @action
    initNode = (dataId) => {
        API_MIND.mindSubData({dataId, ...this.params}).then((data) => {
            let subData = data.subData;
            var PNode = this._jm.get_node(dataId);
            this._jm.enable_edit();
            for (let i = 0; i < subData.length; i++) {
                let node = subData[i];
                let id = node.id;
                let topic = node.topic;
                let count = node.count;
                let title = node.title;
                this.nodeList[id] = {id, parent, count, loaded: false, topic, title}
                this._jm.add_node(PNode, id, topic);
            }
            this._jm.disable_edit();
            this.nodeList[dataId].loaded = true;
        });
    }


}
